package com.huawei.M01.iterator.M24;

import java.util.*;
import java.util.stream.Collectors;

public class MostFrequentEven {
    public int mostFrequentEven(int[] nums) {
        Map<Integer, Integer> cache = new TreeMap<>();
        for (int num : nums) {
            if (num % 2 == 0) {
                cache.put(num, cache.getOrDefault(num, 0) + 1);
            }
        }
        List<Integer> collect = cache.values().stream().sorted().collect(Collectors.toList());
        for (Map.Entry<Integer, Integer> entry : cache.entrySet()) {
            if (Objects.equals(entry.getValue(), collect.get(collect.size() - 1))) {
                return entry.getKey();
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(new MostFrequentEven().mostFrequentEven(new int[]{29,47,21,41,13,37,25,7}));
    }
}
